 
package com.xidian.zhiancloud.util.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {
	public static DateFormat getDate(Locale locale) {
		return getDate(locale, null);
	}

	public static Date getDate(String sDate) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = null;
		try {
			d = df.parse(sDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d;
	}
	
	public static DateFormat getDate(Locale locale, TimeZone timeZone) {
		DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);

		if (timeZone != null) {
			df.setTimeZone(timeZone);
		}

		return df;
	}

	public static DateFormat getDateTime(Locale locale) {
		return getDateTime(locale, null);
	}

	public static DateFormat getDateTime(Locale locale, TimeZone timeZone) {
		DateFormat df = DateFormat.getDateTimeInstance(
			DateFormat.SHORT, DateFormat.SHORT, locale);

		if (timeZone != null) {
			df.setTimeZone(timeZone);
		}

		return df;
	}

	public static DateFormat getTime(Locale locale) {
		return getTime(locale, null);
	}

	public static DateFormat getTime(Locale locale, TimeZone timeZone) {
		DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, locale);

		if (timeZone != null) {
			df.setTimeZone(timeZone);
		}

		return df;
	}

	public static final String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";

	public static int compareTo(Date date1, Date date2) {

		// Workaround for bug in JDK 1.5.x. This bug is fixed in JDK 1.5.07. See
		// http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=6207898 for
		// more information.

		if ((date1 != null) && (date2 == null)) {
			return -1;
		}
		else if ((date1 == null) && (date2 != null)) {
			return 1;
		}
		else if ((date1 == null) && (date2 == null)) {
			return 0;
		}

		long time1 = date1.getTime();
		long time2 = date2.getTime();

		if (time1 == time2) {
			return 0;
		}
		else if (time1 < time2) {
			return -1;
		}
		else {
			return 1;
		}
	}

	public static String getCurrentDate(String pattern, Locale locale) {
		return getDate(new Date(), pattern, locale);
	}

	public static String getDate(Date date, String pattern, Locale locale) {
		DateFormat dateFormat = new SimpleDateFormat(pattern, locale);

		return dateFormat.format(date);
	}

	public static int getMinutesBetween(
		Date startDate, Date endDate, TimeZone timeZone) {

		Calendar startCal = new GregorianCalendar(timeZone);

		startCal.setTime(startDate);

		Calendar endCal = new GregorianCalendar(timeZone);

		endCal.setTime(endDate);

		int i = 0;

		while (startCal.before(endCal)) {
			startCal.add(Calendar.MINUTE, 1);

			if (startCal.before(endCal)) {
				i++;
			}
		}

		return i;
	}
	
	public static int getMinutesBetweenForDefault(
			Date startDate, Date endDate) {

			Calendar startCal = new GregorianCalendar();

			startCal.setTime(startDate);

			Calendar endCal = new GregorianCalendar();

			endCal.setTime(endDate);

			int i = 0;

			while (startCal.before(endCal)) {
				startCal.add(Calendar.MINUTE, 1);

				if (startCal.before(endCal)) {
					i++;
				}
			}

			return i;
		}
	
	public static Date StringToDate(String dateStr,String formatStr){
		DateFormat dd=new SimpleDateFormat(formatStr);
		Date date=null;
		try {
			date = dd.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * For Mysql
	 * 数据库Datetime类型入库时对应java Timestamp类型
	 * 获取对应时间字串
	 * @return
	 */
	public static String DbDateForMysql(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}
	
	public static String formatDateForMysql(Date date){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}
	
	/**
	 * 日期进行加减操作
	 * @param date 
	 * @param field 加减的日期处理字段(Calendar.MONTH.....)
	 * @param value 加减值
	 * @return
	 */
	public static Date DateAdd(Date date, int field, int value){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, value);
		return cal.getTime();
	}
	
	/**
	 * 获取field对应的年/月/日
	 * @param date
	 * @param field(Calendar.YEAR/Calendar.MONTH/Calendar.DAY)
	 * @return
	 */
	public static int getYMDFromDate(Date date, int field){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(field);
	}
	
	public static long getDatelineLong(){
		return (long)(new Date()).getTime()/1000;
	}
	
	/**
	 * 判断日期是否在规定的时间内
	 * @param sizetime 日期
	 * @param datetimesize 相差多少分钟
	 * @return 
	 */
	public static boolean getBetweenDayNumber(String sizetime,long datetimesize){
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now = null;
		Date date = null;
		if(sizetime.length()<19){
			sizetime = sizetime + ":00";
		}
		try{
			now = df.parse(dateString);
			date = df.parse(sizetime);
		}catch(Exception e){
		}
		long l = now.getTime() - date.getTime();
		//long day = l / (24 * 60 * 60 * 1000);
		//long hour = (l / (60 * 60 * 1000) - day * 24);
		//long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
		//long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		long mins = (l / (60 * 1000));//相隔多少分钟
		boolean strtime = false;
		if(mins<datetimesize){
			strtime = true;
		}
		return strtime;
	}
	public static void main(String [] args) throws Exception{
		System.out.println(DateAdd(new Date(), Calendar.MONTH, 1));
	}
}
